﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using DAL;
using BLL;

    /// <summary>
    /// Summary description for AddCartSV
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [ScriptService]
    public class AddCartSV : System.Web.Services.WebService
    {

        public AddCartSV()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        [WebMethod(EnableSession = true)]
        public bool AddSesson(string prdID, int qty)
        {
            if (qty == 0) qty = 1;
            ProductBLL data = new ProductBLL();
            //Product itemDO = new Product();
            //itemDO.Id = prdID.ToString();
            //itemDO.ProductID = idProduct;
            Product product = data.GetById(prdID);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.Id;
                item.Image = product.Image_01;
                item.Name = product.Name;
                if (product.Cost_Sale != null) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    item.Cost = (decimal)product.Cost_Sale;
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    item.Cost = (decimal)product.Cost;
                }
                item.Quantity = qty;
                ((MyShopCart)Session["shop"]).Add(item);
            }
            return true;
        }

        [WebMethod(EnableSession = true)]
        public string GetByStringEmail(string email)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                string ok = "";
                if (email == "")
                    return "";
                else
                    ok = data.Users.Where(p => p.Email == email).FirstOrDefault().Email;
                return ok;
            }
        }


        [WebMethod(EnableSession = true)]
        public string rtTotal()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return string.Format("{0:0,0}", cart.GetTotalCost());
        }
        [WebMethod(EnableSession = true)]
        public string rtNumber()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return cart.GetTotalProduct().ToString();

        }

    }
